package com.yesido.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yesido.pojo.User;
import com.yesido.springcloud.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: my-study
 * @description:
 * @author: yuliang
 * @create: 2021-06-07 16:10
 */
@RestController
@RequestMapping("/pd1")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findUserById/{id}")
    // 5s内20次服务调用失败 服务熔断 出现异常的时候调用别的
    @HystrixCommand(fallbackMethod = "hystrixFindUserById")
    public User findAllUsers(@PathVariable("id") int id) {
        User user = userService.findUserById(id);
        if (user == null) {
            throw new RuntimeException("查询为空。。。");
        }
        return user;
    }

    //备选方法
    public User hystrixFindUserById(@PathVariable("id") int id) {
        return new User().
                setUserName("null").
                setDbNames("query is null ->,mysql query is null--@Hystrix");
    }

}